# Copyright (c) 2019. The YARA Authors. All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


COPTS = [
    "-D_GNU_SOURCE",
    # Tell the compiler we want the C99 standard.
    "-std=c99",
]

cc_library(
    name = "blob",
    hdrs = ["blob.h"],
)

cc_library(
    name = "util",
    srcs = ["util.c"],
    textual_hdrs = ["util.h"],
    deps = ["@//:libyara"],
)

cc_test(
    name = "test_alignment",
    srcs = ["test-alignment.c"],
    copts = COPTS,
    deps = ["@//:libyara"],
    linkstatic=True,
)

cc_test(
    name = "test_arena",
    srcs = ["test-arena.c"],
    copts = COPTS,
    deps = [
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

cc_test(
    name = "test_atoms",
    srcs = ["test-atoms.c"],
    copts = COPTS,
    deps = [
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

cc_test(
    name = "test_rules",
    srcs = ["test-rules.c"],
    copts = COPTS,
    data = [
        "data/base64",
        "data/baz.yar",
        "data/foo.yar",
        "data/include/bar.yar",
        "data/xor.out",
        "data/xorwide.out",
        "data/xorwideandascii.out",
    ],
    deps = [
        ":blob",
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

cc_test(
    name = "test_pe",
    srcs = ["test-pe.c"],
    copts = COPTS,
    data = [
        "data/079a472d22290a94ebb212aa8015cdc8dd28a968c6b4d3b88acdd58ce2d3b885",
        "data/079a472d22290a94ebb212aa8015cdc8dd28a968c6b4d3b88acdd58ce2d3b885.upx",
        "data/tiny",
        "data/tiny-idata-51ff",
        "data/tiny-idata-5200",
        "data/tiny-overlay",
        "data/weird_rich",
        "data/mtxex.dll",
        "data/mtxex_modified_rsrc_rva.dll",
    ],
    deps = [
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

cc_test(
    name = "test_elf",
    srcs = ["test-elf.c"],
    copts = COPTS,
    deps = [
        ":blob",
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

cc_test(
    name = "test_api",
    srcs = ["test-api.c"],
    data = ["data/baz.yar"],
    copts = COPTS,
    deps = [
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

cc_test(
    name = "test_bitmask",
    srcs = ["test-bitmask.c"],
    copts = COPTS,
    deps = [
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

cc_test(
    name = "test_math",
    srcs = ["test-math.c"],
    copts = COPTS,
    deps = [
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

cc_test(
    name = "test_stack",
    srcs = ["test-stack.c"],
    copts = COPTS,
    deps = [
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

# This test fails when running under ASAN
cc_test(
    name = "test_exception",
    srcs = ["test-exception.c"],
    copts = COPTS,
    deps = [
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

cc_test(
    name = "test_macho",
    srcs = ["test-macho.c"],
    data = [
        "data/tiny-universal",
        "data/tiny-macho",
    ],
    copts = COPTS,
    deps = [
        ":blob",
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

cc_test(
    name = "test_dex",
    srcs = ["test-dex.c"],
    copts = COPTS,
    deps = [
        ":blob",
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)

cc_test(
    name = "test_re_split",
    srcs = ["test-re-split.c"],
    copts = COPTS,
    deps = [
        ":util",
        "@//:libyara",
    ],
    linkstatic=True,
)
